package com.yixun.controller;

import com.yixun.entity.YxDept;
import com.yixun.service.DeptService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/dept")
public class DeptController extends BaseController {

    private final DeptService deptService;

    public DeptController(DeptService deptService) {
        this.deptService = deptService;
    }

    @RequestMapping(value = "/list")
    public String list(Model model){
        model.addAttribute("dept_id", this.getSessionUser().getDept_id());
        return "org/dept/deptList";
    }

    /**
     * 获取部门树
     * @param dept_id
     * @return
     */
    @RequestMapping(value = "/getDeptTreeJsonStr/{dept_id}")
    @ResponseBody
    public String getDeptTreeJsonStr(@PathVariable(value = "dept_id") String dept_id){
        return deptService.getDeptTree(dept_id);
    }

    /**
     * 根据部门id获取子部门信息
     * @param dept_id
     * @return
     */
    @RequestMapping(value = "/getChildDeptListByID/{dept_id}")
    @ResponseBody
    public List<YxDept> getChildDeptListByID(@PathVariable(value = "dept_id") String dept_id){
        return deptService.getChildDeptListByID(dept_id);
    }

    /**
     * 跳转部门添加页面
     * @param parentID
     * @param model
     * @return
     */
    @RequestMapping(value = "/go_dept_add")
    public String goDeptAdd(String parentID, String dept_id, Model model){
        model.addAttribute("parentID", parentID);
        if (null != dept_id && dept_id != ""){
            model.addAttribute("dept_id", dept_id);
        } else {
            model.addAttribute("dept_id", "null");
        }
        return "org/dept/dept_add";
    }

    /**
     * 添加部门
     * @param dept
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public boolean add(@RequestBody YxDept dept){
        int row = deptService.insertDept(this.getSessionUser(), dept);
        if (row == 1) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 根据部门id获取部门信息
     * @param dept_id
     * @return
     */
    @RequestMapping(value = "/getDeptBean/{dept_id}")
    @ResponseBody
    public YxDept getDeptBean(@PathVariable(value = "dept_id") String dept_id){
        return deptService.getDeptBean(dept_id);
    }

    /**
     * 更新部门信息
     * @param dept
     * @return
     */
    @RequestMapping(value = "edit", method = RequestMethod.POST)
    @ResponseBody
    public boolean edit(@RequestBody YxDept dept){
        int row = deptService.updateDept(this.getSessionUser(), dept);
        if (row == 1) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 删除部门信息
     * @param id
     * @return
     */
    @RequestMapping(value = "del", method = RequestMethod.POST)
    @ResponseBody
    public boolean del(String id){
        int row = deptService.delDept(this.getSessionUser(), id);
        if (row > 0) {
            return true;
        } else {
            return false;
        }
    }


    /**
     * 跳转单选部门页面
     * @param handle_name
     * @return
     */
    @RequestMapping(value = "/go_select_single_dept")
    public String go_select_single_dept(String handle_name, String user_id ,Model model){
        model.addAttribute("handle_name", handle_name);
        model.addAttribute("dept_id",this.getSessionUser().getDept_id());
        model.addAttribute("user_id", user_id);
        return "org/dept/select_single_dept";
    }

    /**
     * 移动部门
     * @param parent_id
     * @param dept_ids
     * @return
     */
    @RequestMapping(value = "/move",method = RequestMethod.POST)
    @ResponseBody
    public boolean move(String parent_id, String dept_ids){
        int row = deptService.moveDept(this.getSessionUser(), parent_id, dept_ids);
        if (row > 0) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 保存 Dept 排序
     * @param map
     * @return
     */
    @RequestMapping(value = "/sort", method = RequestMethod.POST)
    @ResponseBody
    public boolean sort(@RequestBody Map<String, String> map){
        int row = deptService.sortDept(this.getSessionUser(), map.get("id"));
        if (row > 0) {
            return true;
        } else {
            return false;
        }

    }

}
